<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

  <div id="app">


  </div>
  <!-- 引入React依赖 -->
  <script src="../React/react.development.js"></script>
  <script src="../React/react-dom.development.js"></script>
  <script src="../React/babel.min.js"></script>

  <!-- 编写React代码 -->
  <script type="text/babel">
    class App extends React.Component {
      constructor() {
        super()
        //定义变量
        this.state = {
          message: "Hello World",
        }


        this.btnClick = this.btnClick.bind(this)
      }
      render() {
        return (
          <div>
            {/* 方案1：bing绑定this（显示绑定） */}
            <button onClick={this.btnClick.bind(this)}>按钮</button>
            <button onClick={this.btnClick}>按钮</button>


            {/* 方案2：定义函数时，使用箭头函数 */}
            <button onClick={this.increment}>+1</button>

            {/* 方案3： */}
            <button onClick={() => { this.decrement() }}>-1</button>
          </div>
        )
      }

      btnClick() {
        console.log(this);
        // console.log(this.state.message);
      }

      increment = () => {
        //箭头函数中永远不绑定this
        console.log(this.state.message);
      }

      decrement() {
        console.log(this.state.message);
      }

    }
  </script>

  <script type="text/babel">
    ReactDOM.render(<App />, document.getElementById("app"))
  </script>

</body>

</html>